﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xpus.YouYTube.Utility;
using Xpus.YouYTube.Data;

namespace Xpus.YouYTube.WebSite.Controllers
{
	public class CommentController : Controller
	{
		public ActionResult CommentSubmit(string svideoId, string userName, string commentBody)
		{
			try
			{
				int videoId = int.Parse(svideoId);
				string ip = Request.UserHostAddress;
				string rawBody = "";

				if (TIpUtility.IsIpBlocked(ip, true))
					return Json(new { IsError = true, Message = "对不起，您的IP地址已被屏蔽！您无法发表评论。" });

				if (commentBody != null)
					rawBody = commentBody.Trim();

				if (rawBody == "")
					return Json(new { IsError = true, Message = "必须填写评论内容。" });

				using (TDataContext db = new TDataContext())
				{
					TComment oc = db.TComments.FirstOrDefault(c =>
						c.VideoId == videoId &&
						c.UserIp == ip &&
						c.AddedTime > DateTime.Now.AddSeconds(-30));
					if (oc != null)
						return Json(new { IsError = true, Message = "30秒内不许再次评论，请稍候再试。" });

					TComment comment = new TComment()
					{
						VideoId = videoId,
						UserName = userName == null ? "" : TSensitiveWordManager.Repleace(userName),
						Body = commentBody == null ? "" : TSensitiveWordManager.Repleace(commentBody),
						AddedTime = DateTime.Now,
						IsDeleted = false,
						UserIp = ip,
					};
					db.TComments.InsertOnSubmit(comment);
					db.SubmitChanges();
					return Json(new { IsError = false, Message = "评论发表成功，谢谢！" });
				}
			}
			catch (Exception ex)
			{
				return Json(new { IsError = true, Message = "发生未知错误：" + ex.Message });
			}
		}

		public ActionResult GetComments(string svideoId, string spageIndex, string spageSize)
		{
			try
			{
				int videoId = int.Parse(svideoId);
				int pageIndex = int.Parse(spageIndex);
				int pageSize = int.Parse(spageSize);
				int totalPages = 0;

				using (TDataContext db = new TDataContext())
				{
					TVideo video = db.TVideos.FirstOrDefault(v => v.Id == videoId && v.IsDeleted == false);

					if (video == null)
						return Json(new { IsError = true, Message = "视频不存在。" });

					var q = from c in db.TComments
							where c.VideoId == videoId
							select c;

					totalPages = (q.Count() + pageSize - 1) / pageSize;

					if (pageIndex < 0)
						pageIndex = totalPages;

					TComment[] comments = q.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToArray();

					return Json(new
					{
						IsError = false,
						PageIndex = pageIndex,
						PageSize = pageSize,
						TotalPages = totalPages,
						Comments = comments.Select(c => new
						{
							Id = c.Id,
							AddedTime = c.AddedTime.ToString("yyyy-MM-dd HH:mm:ss"),
							UserName = HttpUtility.HtmlEncode(c.UserName),
							Body = c.IsDeleted ? "[该楼已被管理员删除。]" : HttpUtility.HtmlEncode(c.Body),
							Anumber = c.AgreeNumber,
							Dnumber = c.DisagreeNumber,
							UserIp = TIpUtility.GetDisplayIp(c.UserIp),
							IsDeleted = c.IsDeleted,
						})
					});
				}
			}
			catch(Exception ex)
			{
				return Json(new { IsError = true, Message = "发生了未知错误：" + ex.Message });
			}
		}
	}
}
